package com.repair.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.repair.common.base.BaseController;
import com.repair.common.exception.AppException;
import com.repair.model.Order;
import com.repair.model.Prod;
import com.repair.model.SysUser;
import com.repair.model.Type;

import cn.hutool.core.codec.Base64;



public class OrderController extends BaseController {
	/**
	 * 修改密码
	 */
	public void changePassword(){
		String userid = getPara("userid");
		String oldpassword = getPara("oldpassword");
		String newpassword = getPara("password");
		SysUser sysuser = SysUser.dao.findById(userid);
		if(!sysuser.getPassword().equals(oldpassword)){
			throw new AppException("旧密码错误！");
		}
		sysuser.setPassword(newpassword);
		sysuser.update();
		renderJsonSuccess();
	}
	/**
	 * 获取所有的order 类型
	 */
	public void getAllOrderType(){
		List<Type> typelist = Type.dao.find("select * from type");
		renderJsonResult(typelist);
	}
	/**
	 * 发单
	 */
	public void addOrder(){
		String userid = getPara("userid");
		SysUser user = SysUser.dao.findById(userid);
		String title = getPara("title");
		String des = getPara("des");
		String type = getPara("type");
		String selectedShop = getPara("selectedShop");
		SysUser resiciver=null;
		if(!StrKit.isBlank(selectedShop)){
			resiciver = SysUser.dao.findById(selectedShop);
		}
		Order order = new Order();
		order.setTitle(title);
		order.setOrdertype(type);
		order.setDes(des);
		order.setSender(userid);
		order.setSendtime(new Date());
		order.setAddress(user.getAddress());
		order.setPhone(user.getPhone());
		order.setOrderid(StrKit.getRandomUUID());
		order.setStatus("0");
		if(resiciver!=null){
			order.setResver(resiciver.getUserid());
			order.setResvtiem(new Date());
		}
		order.save();
		renderJsonSuccess();
	}
	/*
	 * 获取顾客未接订单
	 */
	public void getCusWjOrder(){
		String userid = getPara("userid");
		List<Order> orderList = Order.dao.find("select * from `repair`.`order` a where a.sender = ? and a.resver is null ",userid);
		renderJsonResult(orderList);
	}
	/**
	 * 获取顾客已接订单
	 */
	public void getCusYjOrder(){
		String userid = getPara("userid");
		List<Order> orderList = Order.dao.find("select * from `repair`.`order` a where a.sender = ? and a.resver is  not null ",userid);
		renderJsonResult(orderList);
	}
	/**
	 * 获取所有的未接订单
	 */
	public void getWjOrder(){
		List<Order> orderList = Order.dao.find("select * from `repair`.`order` a where  a.resver is  null ");
		renderJsonResult(orderList);
	}
	/**
	 * 获取商家已接订单
	 */
	public void getBusiYjOrder(){
		String userid = getPara("userid");
		List<Order> orderList = Order.dao.find("select * from `repair`.`order` a where   a.resver =? ",userid);
		renderJsonResult(orderList);
	}
	/**
	 * 商家接单
	 */
	public void JieDan(){
		String userid = getPara("userid");
		String orderid = getPara("orderid");
		Order order = Order.dao.findById(orderid);
		if(order.getResver()!=null){
			throw new AppException("该单已被接!");
		}
		order.setResver(userid);
		order.setResvtiem(new Date());
		order.update();
		renderJsonSuccess();
		
	}
	/**
	 * 完成订单
	 */
	public void completeOrder(){
		String orderid = getPara("orderid");
		Order order = Order.dao.findById(orderid);
		order.setStatus("1");
		order.update();
		renderJsonSuccess();
	}
	/**
	 * 获取订单详细信息
	 */
	public void orderDetail(){
		String orderid = getPara("orderid");
		StringBuilder sqlBF = new StringBuilder();
		sqlBF.append(" SELECT                                                            ");
		sqlBF.append(" 	a.address,                                                       ");
		sqlBF.append(" 	a.des,                                                           ");
		sqlBF.append(" 	a.orderid,                                                       ");
		sqlBF.append(" 	type.typedes,                                                    ");
		sqlBF.append(" 	a.phone,                                                         ");
		sqlBF.append(" 	a.resvtiem,                                                      ");
		sqlBF.append(" 	a.sendtime,                                                      ");
		sqlBF.append(" 	a.title,                                                         ");
		sqlBF.append("  a.star, ");
		sqlBF.append("  a.comment, ");
		sqlBF.append(" 	cus.`name` as sendername,                                                      ");
		sqlBF.append(" 	busi.`name` as resvername,                                                     ");
		sqlBF.append(" 	busi.`address` as resveraddress,                                                     ");
		sqlBF.append(" 	busi.`phone` as resverphone,                                                     ");
		sqlBF.append(" 	CASE                                                             ");
		sqlBF.append(" WHEN a.`status` = '0' THEN                                          ");
		sqlBF.append(" 	'未完成'                                                         ");
		sqlBF.append(" ELSE                                                              ");
		sqlBF.append(" 	'已完成'                                                         ");
		sqlBF.append(" END AS status                                                     ");
		sqlBF.append(" FROM                                                              ");
		sqlBF.append(" 	`repair`.`order` a                                               ");
		sqlBF.append(" LEFT JOIN `repair`.sys_user cus ON (a.sender = cus.userid)        ");
		sqlBF.append(" LEFT JOIN `repair`.sys_user busi ON (a.resver = busi.userid),     ");
		sqlBF.append("  `repair`.type type                                               ");
		sqlBF.append(" WHERE                                                             ");
		sqlBF.append(" 	a.orderid = ?                                                    ");
		sqlBF.append(" AND a.ordertype = type.typecode                                     ");
		Record record  = Db.findFirst(sqlBF.toString(),orderid);
		renderJsonResult(record);
	}
	public void getProdList(){
		String userid = getPara("userid");
		List<Prod> prodList = Prod.dao.find(" SELECT * from prod a where a.userid = ? ",userid);
		renderJsonResult(prodList);
	}
	public void prodAdd(){
		String base64data = HttpKit.readData(getRequest());
		String userid = getPara("userid");
		String name = getPara("name");
		String price = getPara("price");
		String des = getPara("des");
		Prod prod = new Prod();
		prod.setUserid(userid);
		prod.setName(name);
		prod.setPrice(price);
		prod.setDesc(des);
		prod.setProdid(StrKit.getRandomUUID());
		String webpath = PathKit.getWebRootPath();
		String newFileName = StrKit.getRandomUUID()+".jpeg";
		Base64.decodeToFile(base64data, new File(webpath+File.separator+"images"+File.separator+"upload"+File.separator+newFileName));
		prod.setImageurl("/images/upload/"+newFileName);
		prod.save();
		renderJsonSuccess();
	}
	
	public void getUserinfo(){
		String userid = getPara("userid");
		SysUser user  = SysUser.dao.findById(userid);
		renderJsonResult(user);
		
	}
	public void updateUser(){
		SysUser user = getRequestBody(SysUser.class);
		user.update();
		renderJsonSuccess();
	}
	public void getShopList(){
		List<SysUser> userList = SysUser.dao.find(" select * from sys_user where type = '1' ");
		renderJsonResult(userList);
	}
	
	public void getShopInfo(){
		String userid = getPara("id");
		List<Prod> prodList = Prod.dao.find(" SELECT * from prod a where a.userid = ? ",userid);
		SysUser user = SysUser.dao.findById(userid);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("prodList", prodList);
		map.put("shopInfo", user);
		renderJsonResult(map);
	}
	public void searchShoplist(){
		String searchtext = getPara("searchtext");
		if(StrKit.isBlank(searchtext)){
			searchtext = "";
		}
		searchtext = "%"+searchtext+"%";
		StringBuilder sqlBF = new StringBuilder();
		sqlBF.append(" SELECT                                    ");
		sqlBF.append(" 	distinct a.*                                      ");
		sqlBF.append(" FROM                                      ");
		sqlBF.append(" 	sys_user a                               ");
		sqlBF.append(" LEFT JOIN prod b ON (a.userid = b.userid) ");
		sqlBF.append(" WHERE                                     ");
		sqlBF.append(" 	(a.`name` LIKE ?                          ");
		sqlBF.append(" OR b. NAME LIKE ?                         ");
		sqlBF.append(" OR a.phone LIKE ?                         ");
		sqlBF.append(" OR a.address LIKE ?                       ");
		sqlBF.append(" OR b.`desc` LIKE ?)                        ");
		sqlBF.append(" and a.type= '1' ");
		List<SysUser> useList = SysUser.dao.find(sqlBF.toString(),searchtext,searchtext,searchtext,searchtext,searchtext);
		renderJsonResult(useList);
	}
	public void savePj(){
		String orderid = getPara("orderid");
		String star =getPara("star");
		String pj =getPara("pj");
		Order order = Order.dao.findById(orderid);
		order.setStar(star);
		order.setComment(pj);
		order.update();
		renderJsonSuccess();
		
	}
}
